<?php

class ApiController extends Controller {

    //public $layout = 'column1';


    public function actionSearchBus() {
        if (Yii::app()->request->isPostRequest && isset($_POST['Bus'])) {

            $aVals = $_POST['Bus'];

            $bIsDirection = isset($aVals['direction']) ? TRUE : FALSE;

            $iBusId = $aVals['bus_id'];

            $aBusStation = Api::getBusStation($iBusId, $bIsDirection);
            $aPolyline = Api::getBusPolyline($iBusId, $bIsDirection);


            echo CJSON::encode(array('station' => $aBusStation, 'polyline' => $aPolyline));
        }
    }

    public function actionPolyline() {
        if (Yii::app()->request->isPostRequest && isset($_POST['Bus'])) {
            if (isset($aVals['direction'])) {
                $criterial->addCondition('direction = ' . BusStation::DIRECTION_TRUE);
            } else {
                $criterial->addCondition('direction = ' . BusStation::DIRECTION_FALSE);
            }
        }

        $criterial->addCondition('bus_id = ' . (int) $aVals['bus_id']);
        $modelPolyline = Polyline::model()->find($criterial);

        echo CJSON::decode($modelPolyline);
    }

    public function actionBus() {
        if (isset($_GET['bus_id'])) {
            if(isset($_GET['direction'])) {
                echo CJSON::encode($this->getBus($_GET['bus_id'], $_GET['direction']));
                exit();
            } else {
                echo CJSON::encode($this->getBus($_GET['bus_id']));
                exit();
            }
            
        }
        
        $bus = Bus::model()->findAll();
        $return = array(
            'data' => $bus,
            'success' => 1
        );
        
        echo CJSON::encode($return);
        exit();
    }

    public function getBus($id, $direction = 1) {
        $id = (int) $id;
        
        //get busdetail id
        $busDetail = BusDetail::model()->find('bus_id = ' . $id . ' AND direction = ' . $direction);
        
        if(!$busDetail) {
            return array(
                'success' => 0,
                'error' => 'Can not find bus',
            );
        }
        
        $busDetailId = $busDetail['bus_detail_id'];
        
        $cacheKey = 'busDetail_' . $busDetailId;
        
        if(!($cache = Yii::app()->cache->get($cacheKey))) {
            $cache = array(
                'station' => array()
            );
            
            //get station
            $criteria = new CDbCriteria();
            $criteria->addCondition('bus_detail_id = ' . $busDetailId);
            $criteria->order = 'ordering ASC';
            $criteria->with = array (
                'station'
            );
            $stationModel = BusStation::model()->findAll($criteria);
            
            foreach($stationModel as $item) {
                $cache['station'][] = array(
                    'station_id' => $item->station_id,
                    'name' => $item->station->name,
                );
            }
            
            //success
            $cache['success'] = 1;
            
            Yii::app()->cache->set($cacheKey, $cache);
        }
        
        return $cache;
    }

    public function actionStation() {
        
    }

}
